package com.controller;

import com.google.gson.Gson;
import com.mapper.UserMapper;
import com.pojo.User;
import com.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

//@FeignClient(value = "my8002-test")
@RestController
@Transactional
public class UserController extends GenericController {
    @Autowired
    public UserService userService;
    @Autowired
    public UserMapper userMapper;
    @RequestMapping("/getAllUser")
    public List<User> getAllUser(HttpServletResponse response, HttpServletRequest request){
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setCharacterEncoding("utf-8");
        List<User> users = userService.selAllUser();
        return users;
    }
    @RequestMapping("/getOne/{id}")
    public User getUser(HttpServletResponse response,  @PathVariable("id") int id){
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setCharacterEncoding("utf-8");
        User user = userService.selUserById(id);
        return user;
    }
    @RequestMapping("/getUser")
    public User getUser(){
        JSONObject inputMessage = getInputMessage();
        System.out.println(inputMessage.toString());
        Map map=new HashMap<String,String>();
        JSONObject o = inputMessage.getJSONObject("user1");
        if ("".equals(o)||o==null) {
            return null;
        }
        Gson gson=new Gson();
        User user = gson.fromJson(o.toString(), User.class);
        System.out.println(user.toString());
        map.put("id",user.getId());
        map.put("username",user.getUsername());
        List<User> users = userService.selUser(map);
//        map.put("password",users.get(0).getPassword());
//        Set<String> set = map.keySet();
//        for (String s : set) {
//            Object o1 = map.get(s);
//            System.out.println(o1);
//
//        }
//        Set set1 = map.entrySet();
//        int i = userService.addUser(map);
//        map.put("id","11");
//        map.put("username","杨安华");
//        int i1 = userMapper.addUser2(map);
//        if (i!=0){
//            System.out.println("插入成功");
//        }
//        else {
//            System.out.println("插入失败");
//        }
        return users.get(0);
    }

}
